/**
 * Created by zdx  on 2016/8/26.
 *
 *
 */

define(['jquery',"controller",'mapController','coordtransform'], function ($) {
    var c = require("controller");
    var mapController = require("mapController");
    var coordtransform=require("coordtransform");
    var plane=function(map,ws) {
        this.flights = {};
        this.map = map;
        //this.infoWindow=this.map.createInfoWindow('',{autoMove:false,closeWhenClickMap:true})
        this.ws=ws
        this.isShowLine = false;
        this.isGuiji = false;
        this.baseLine=0;
    };
    /* 初始化某个飞机 */
    plane.prototype.initFlight=function (op) {
        var _this=this;
        var id = op.id;
        var flights=this.flights;

        if (flights[id]) {
            flights[id].point=op;
            this.run(op);

        } else {
            var text=mapController.get_popup_txt(op);
            //var html='<div><div class="amap-info-content amap-info-outer">'+text+'</div><a data-id="'+op.id+'" class="my-info-close amap-info-close" href="javascript: void(0)" style="right: 5px;">×</a><div class="amap-info-sharp" style="height: 23px;"></div></div>'
            flights[id] = {
                marker: this.map.createMark({
                    lnglat: {
                        lat: op.lat * 1,
                        lng: op.lng * 1
                    }
                }),
                lines:[{
                    lat: op.lat * 1,
                    lng: op.lng * 1
                }],
                selIcon:"../../images/airplanesel.png",
                norIcon:"../../images/airplanenor.png",
                showLine:false,
                point:op,
                isCheck:false
            };

            /* 控件一些属性的定义 */
            flights[id].marker.setIcon(flights[id].norIcon,[32,32]);
            flights[id].marker.createInfoWindow(_this.map,text);
            flights[id].marker.on('click', function (e) {
                if(!flights[id].isCheck){
                    flights[id].marker.setIcon(flights[id].selIcon,[32,32]);
                    flights[id].marker.showInfoWindow();
                }else{
                    flights[id].marker.setIcon(flights[id].norIcon,[32,32]);
                    flights[id].marker.hideInfoWindow();
                }
                flights[id].isCheck=!flights[id].isCheck;

            });

            /*if(!this.isGuiji){
                flights[id].lines=[];
                var points=this.ws.get_all_track(id);
                $.each(points,function (i,point) {
                    flights[id].lines.push({lng:point.lng,lat:point.lat});
                });
                this.drawLineById(id);
                this.hideLineById(id);
            }*/


            this.map.addOverlay(flights[id]['marker']);
        }
    };
    /* 飞机运行 */
    plane.prototype.run = function (op) {
        var _this=this;
        var  flights=this.flights;
        var id=op.id;
        var flight = flights[id];
        var nowLngLat={
            lat: op.lat * 1,
            lng: op.lng * 1
        };
        var text=mapController.get_popup_txt(op);
       // var html='<div><div class="amap-info-content amap-info-outer">'+text+'</div><a data-id="'+op.id+'" class="my-info-close amap-info-close" href="javascript: void(0)" style="right: 5px;">×</a><div class="amap-info-sharp" style="height: 23px;"></div></div>'
        /*if(_this.isGuiji){
            flight.lines.push(nowLngLat);
            if(flight.lines.length==300){
                flight.lines=this.ws.get_track()
            }
            if (_this.isShowLine) {
                _this.drawLineById(id);
            }
        }*/
        if (_this.isShowLine) {
            flight.lines.push(nowLngLat);
            if(flight.lines.length>(500+this.baseLine)){
                _this.refresh(id,flight);
            }
            _this.drawLineById(id);
        }


        flight.marker.setRotation( mapController.rotate(flight.marker.getPosition(),nowLngLat));
        flight.marker.setPosition( nowLngLat);
        if(flight.isCheck){
            flight.marker.setIWcontent(text);
        }




    };

    plane.prototype.refresh=function (id,flight) {
        if(this.isGuiji){
            flight.lines=this.ws.get_track();
        }else{
            flight.lines=[];
            var points=this.ws.get_all_track(id);
            $.each(points,function (i,point) {
                flight.lines.push({lng:point.lng,lat:point.lat});
            });
        }
        this.baseLine=flight.lines.length;
    };
    plane.prototype.GJrefresh=function () {
        var _this=this;
        $.each(this.flights,function (id,flight) {
            _this.refresh(id,flight);
        })
    };


    /* var path=[];
     var points=this.ws.get_track(did);
     $.each(points,function (i,point) {
         path.push({lng:point.lng,lat:point.lat});
     });*/

    plane.prototype.drawLineById=function (did) {
        var flights=this.flights;
        var path=flights[did].lines;
        if (flights[did].lineMarker) {
            flights[did].lineMarker.setPath(path);
        }else{
            flights[did].lineMarker = this.map.createPolyline(path,{
                strokeColor: "#FF33FF",
                strokeOpacity: 0.5,
                strokeWeight:1

            });
            this.map.addOverlay(flights[did].lineMarker);
        }
    };
    plane.prototype.hideLineById=function (did) {
        var flights=this.flights;
        if(flights[did].lineMarker){
            flights[did].lineMarker.hide();
        }
    };
    plane.prototype.showLineById=function (did) {
        var flights=this.flights;
        if(flights[did].lineMarker){
            this.refresh(did,flights[did]);
            flights[did].lineMarker.show();
        }
    };
    plane.prototype.remove=function () {
        var mks=[];
        $.each(this.flights,function (i,flight) {
            mks.push(flight.marker);
            if(flight.lineMarker) {
                mks.push(flight.lineMarker);
            }
        });
        this.map.remove(mks);
        this.flights={}
    };

    var drawSpaceLine=function (map,types) {
        this.spaceLinesInfos=null;
        this.types=types;
        this.map=map;
        this.windowInfo=map.createInfoWindow();
        this.get();


    };
    drawSpaceLine.prototype.get=function () {
        var _this=this;
        c.ajax({
            url:'moniter/index',
            data:{
            },
            success:function (result) {
                if(result.err==0){
                    _this.spaceLinesInfos = result.items;

                    _this.drawSpaceLineDetails();
                }else{

                }
            }
        });
    };
    drawSpaceLine.prototype.drawSpaceLineDetails=function () {
        var _this=this;
        $.each(this.spaceLinesInfos,function (i,item) {

            var property =JSON.parse(item.property);
            $.each(property.path,function (i,v) {
                var gj= coordtransform.wgs84togcj02(mapController.changeDMSToLng(v.lng),mapController.changeDMSToLat(v.lat));
                v.lng=gj[0];
                v.lat=gj[1];
            });
            switch(item.type)
            {
                case 254: //画航线

                    item.space=_this.map.createPolyline(property.path,{
                        strokeColor: '#e63f00',
                        strokeOpacity: 1,
                        strokeWeight: 3,
                        zIndex:10,
                        title:item.name,
                        strokeStyle: "solid"
                    });

                    break;
                case 255: // 多边形空域
                    item.space=_this.map.createPolygon(property.path,{
                        fillColor:'#289BEC',
                        fillOpacity:0.4,
                        strokeColor:'#40A4FB',
                        zIndex:10,
                        title:item.name,
                        strokeWeight:2
                    });
                    break;
                case 253:// 圆形空域
                    item.space=_this.map.createCircle(property.path,{
                        fillColor:'#289BEC',
                        fillOpacity:0.4,
                        strokeColor:'#40A4FB',
                        zIndex:10,
                        radius:property.radius*1,
                        title:item.name + '<br/>半径为: '+property.radius+'米',
                        strokeWeight:2
                    });
                    break;
                case 0://起降点
                    item.space=_this.map.createMark({lnglat:property.path[0],title:item.name});
                    item.space.setIcon('../../images/marker_smart_packing.png',[16,16]);
                    break;
                case 1://途径点
                    item.space=_this.map.createMark({lnglat:property.path[0],title:item.name});
                    item.space.setIcon('../../images/marker_smart_packing.png',[16,16]);
                    break;
                case 2://加油咪
                    item.space=_this.map.createMark({lnglat:property.path[0],title:item.name});
                    item.space.setIcon('../../images/marker_smart_packing.png',[16,16]);
                    break;
                case 3://机场
                    item.space=_this.map.createMark({lnglat:property.path[0],title:item.name});
                    item.space.setIcon('../../images/airport.png',[16,16]);

                    break;
                default:

            }
            if(item.space){
                item.space.on('click', function (e) {
                    _this.windowInfo.setContent(item.name);
                    _this.windowInfo.open(_this.map, e);

                });
                _this.map.addOverlay(item.space);
                if($.inArray(item.type,_this.types)==-1){
                    item.space.hide();
                }
            }


        });
    };
    drawSpaceLine.prototype.setTypes=function (Types) {
        this.types=Types;
    };

    drawSpaceLine.prototype.show=function (Type) {
        $.each(this.spaceLinesInfos,function (i,item) {
            if(item.type==Type){
                item.space.show();
            }
        })
    };
    drawSpaceLine.prototype.hide=function (Type) {
        $.each(this.spaceLinesInfos,function (i,item) {
            if(item.type==Type){
                item.space.hide();
            }
        })
    };

    return {
        plane:plane,
        drawSpaceLine:drawSpaceLine
}
});